/* All the quantities are defined in SI-units unless specifically stated. */

#ifndef  SCENE_H

# define   SCENE_H
# define   H_FILE  "scene.h"

# include  <stdio.h>

/* Overall, parallelization can be controlled using this parameter. */
# ifdef _OPENMP
#    define   _OPENMP_
# endif

/* Paralallization of a function can be controlled using these parameters. */
# ifdef _OPENMP_
#    define   _OMP_VEL_ADVANCE_
#    define   _OMP_VEL_BC_
#    define   _OMP_ORLANSKY_OUTLET_
#    define   _OMP_SOLVE_POISSON_
#    define   _OMP_PRESS_BC_
#    define   _OMP_TEMP_ADVANCE_
#    define   _OMP_TEMP_BC_
# endif

/* This is used to define maximum length of the error string corresponding to "errno" in errno.h. */
# define SYSERR_STR_LEN 200

/* This flag sets what version of the solve_poisson should be called. If it is defines, OpenMP version is called. */
// # define CALL_SOLVE_POISSON_OMP

# define   SOLVE_ENERGY

/* If this parameter is defined, various configuration parameters need to be confirmed by user in the
   begining of the program. */
// # define   CHECK_CONFIG

/* Maximum filename length. */
#  define   MAX_FNAME_LEN    50

/* It should be zero if the program statrs from begining. It can be 0, 1, 2,....9. */
#  define   RUN_CNT  0

/* This specifies if the initial condition should be read from a recovery-file or not. */
// #  define   READ_RECOV_FILE

/* Recovery file: data may be read from this file as initial condition for velocity, temperature and pressure fields.*/
#  define   RECOV_FILE          "recov"

/* If poisson equation takes more than this number of iteration for convergence, a recovery file is written. */
#  define   RECOVF_POISS_CNT    100000

#  ifdef READ_RECOV_FILE
 /* 200 the maximum length of a line in the 'RECOV_FILE' */
#   define MAX_RECOVF_LINELEN  200
#  else
 /* The initial time-offset when calculation starts (in second). */
#   define t_INIT    0.0

 /* The constant time-step for time marching (in Seconds). */
#   define DELTAt    2.0E-5
#  endif

/* The maximum time until which the calculation has to continue (in seconds). */
# define tMAX        7200.0

/* After the time tFRAC, the flow rate reaches to wFRAC-th of the maximum possible flow-rate.
   For example, if Q(t = 0) = 0; and, Q(t = infinity) = Qmax;  then, Q(t = tFRAC) = wFRAC*Qmax.
   This also implies that: W_center(t = tFRAC) = wFRAC*W_center(t = t_infinity). */
# define tFRAC  1.0    /* In Seconds. */
# define wFRAC  0.99

//# define   r_UNIF_GRID
# define   th_UNIF_GRID
//# define   z_UNIF_GRID

/* Files in which grid distances are stored. */
# define   R_FILE           "r-grids"
# define   TH_FILE          "th-grids"
# define   Z_FILE           "z-grids"

/* If this variable is defined, the grids are read from the file R_FILE, TH_FILE and Z_FILE. */
// # define   READ_GRID_FILES

/* If any number is less than 'NEG_DELTA', it is consideres as zero. All the quantities should be normalized
   with respect to some suitable reference quantity before being compared with NEG_DELTA. */
// # define   NEG_DELTA        1.0E-10

/* Various files used to record statistics and informations during program run.
   NULL_DEV     :  Path to the null-device of the system.
   LOG_FILE     :  To store essential information like number of grids, grid-size etc.
   TRACE_FILE   :  To store debug informations. Useful while debugging if there is something wrong with the program.
                   If this is not defined, no file is printed.
   DISPLAY_FILE :  To store residual during pressure itterations.

   If any of these files are defined to (char *)0, the output is set to "stderr". If these are defined to NULL_DEV,
   the output is redirected to null-device. */
# define   NULL_DEV         "/dev/null"    /* This is path to the null-device of the system. */
# define   LOG_FILE         "log.txt"      /* NULL_DEV, "log.txt",     (char *)0 */
// # define   TRACE_FILE       (char *)0    /* NULL_DEV, "trace.txt",   (char *)0 */
# define   DISPLAY_FILE     (char *)0  /* NULL_DEV, "display", (char *)0 */

/* File to store different iteration-parameters at every time-step. */
# define   ittr_RES_FILE       "ittr" /* "itterations.dat" */

/* Frequency with which statistic-files are written.
  The result file will be printed for the "t_cnt" values which are multiple of the RESF_FREQ variable defined below.
  e.g. if RESF_FREQ = 3, the result files would be printed for time steps 1, 3, 6, 9, 12, ...... and so on.
  Similarly for DFILE_FREQ corresponds to DISPLAY_FILE and IRFILE_FREQ corresponds to ittr_RES_FILE. */
# define   RESF_FREQ    5000
# define   DFILE_FREQ   20000
# define   IRFILE_FREQ  20000

/* This is the frequency with which the residual will be displayed on screen for monitoring
  the convergence rate while solving the Poisson's Equation. */
# define   DISP_FREQ    100

/* This is added to all the result files. */
# define   RES_FILE    "res"

/* Number of dependent variables. Do not edit it. */
# define   DEPVAR_N  4 /* u, v, w, P */
# ifdef    SOLVE_ENERGY
# undef    DEPVAR_N
# define   DEPVAR_N  5 /* u, v, w, P, T */
# endif

/* Runge-Kutta related parameters. Do not edit it arbitariry. */
# define   nRK   3    /* No. of stages and order of the RK method. */
# define   c2    (1.0/6)  /* The free parameter used to determine the co-efficients. */
# define   a31   (2.0/3 - 2.0/(9*c2))
# define   a32   (2.0/(9*c2))
# define   a41   0.25
# define   a42   0
# define   a43   0.75
# define   b1    a41
# define   b2    a42
# define   b3    a43
# define   d1    (-2+1.0/c2)
# define   d2    (-1.0/c2)
# define   d3    3.0


/* Pressure-ralaxation-factor (prf)'s value adjustment parameters: For each time-step, it starts with an initial
  guess-value 'PRF_INIT'. At this time-step, at an iteration, if (p_new - p_old)_rms value through all the cells
  is less than the value at previous iteration, 'prf'-value is increased by 'PRF_DELTA'; if it is more, it is
  decreased by 'PRF_DELTA'. This rule is applied only if the new expected 'prf'-value lies within 'PRF_MIN' and
 'PRF_MAX'. If it goes out of this range, the 'prf'-value is left unchanged and a warning message is printed. */
# define   PRF_INIT      1.825
# define   PRF_DELTA     0.0
# define   PRF_MAX       1.3
# define   PRF_MIN       0.7

/* If pressure-residual during solution of the Poisson's equation in 'solve_poisson()' remains less than 'PRESS_DELTA'
   for 'POISS_CNT_MIN' times continuously, we say that the Poisson Equation is solved. This also implies that the
   "p_cnt" returned by 'solve_poisson()' will never be less than 'POISS_CNT_MIN'.

   "POISS_CNT_MIN >= max(nr, nth, nz)" should be used.

       If the above condition doesn't meet within 'POISS_CNT_MAX' iterations, we say that Poisson Equation
   could not be solved as expected. The details can be found in 'T_RES_FILE'. */
# define   PRESS_DELTA      5.0E-4
# define   POISS_CNT_MIN    10
# define   POISS_CNT_MAX    200000

/* "solve_poisson_omp()" ignores my this flag because this function always uses JACOBI itteration method.\
    However, "solve_poisson()" can be controlled using it. */
// # define  PPE_ITTR_JACOBI


/* If the initial-residual during solution of the Poisson Equation is less than "PRESS_DELTA" (i.e. "solve_poisson()"
   returns "POISS_STOP_CNT") for "SST_CNT_MAX" times continuously, the CFL-number is increased.
        If Poisson Equation doesn't converge within "POISS_CNT_MAX" for "DIV_CNT_MAX" times continuously, we say that
   the solution is diverging and try to get it converged by decreasing the CLF-number.

   The aforementioned increament or decreament in CFL-number is done only if "CLF_MIN <= cfl <= CLF_MAX". If this
   condition doesn't hold true, the solution is assummed to reached steady state or diverged respectively and the
   program is stopped.

   To disable usage of these, set these to a negative number.
*/
# define   SST_CNT_MAX      -1
# define   DIV_CNT_MAX      1

/*
# ifndef  DELTAt
#   define   CFL_INIT      0.02
#   define   CFL_MIN       0.01
#   define   CFL_MAX       0.1
#   define   CFL_DELTA     0.001
# endif*/


# ifdef SOLVE_ENERGY
 /* This factor controls the dissipation added to convective terms of
    the energy equation.
    If EEQ_DISSIP_FACT = 1, it corresponds to full upwinding scheme.
    If it is zero, it corresponds to full central difference scheme.
    If it is undefined, its value is considered as '1' and the convective
    term is discretized in simplified way. */
#   define  EEQ_DISSIP_FACT  0.5



#   define  ABS_ZERO     273.0  /* in Kelvin. */
#   define  T_INIT       (21.5 + ABS_ZERO)  /* Initial temperature in Kelvin. */
#   define  T_INLET      (21.5 + ABS_ZERO)  /* Inlet temperature in Kelvin. */


 /* If VOLUME_HSRC = 'y' is defined, the heat-source-term will be added to the energy-equation, otherwise, not. */
#   define  VHSRC_FLAG   'n'
#   define  EXPECTED_T_RISE  5.0  /* In Kelvin */
#   define  EXPECTED_TIME    600.0  /* In Sec */


/* All the solid surfaces are considered to be insulating excluding the central region of the top-surface where
   a constant heat-flux heater is attached. All the heat-fluxes are in "W/m^2". The heater is assummed to be on
   after "t_HEATER_ON" seconds from begining of the flow. */
#   define  t_HEATER_ON      0.0
#   define  TOP_HEATER_HF    20.0E+3
#   define  TOP_ANNULUS_HF   0.0
#   define  SIDE_WALL_HF     0.0
#   define  SOLID_BASE_HF    0.0

 /* These three parameters defines if Kinematic Viscosity, Density and Thermal Conductivity of water are temperature-dependent or not. */
#   define  KVISC_TDEP
#   define  DENS_TDEP
#   define  THMCOND_TDEP

/* If RETURN_CONST_KVISC and RETURN_CONST_DENS are defined, the functions kvisc() and dens() will return
   values at T = T_INIT. *//*
#   ifdef  KVISC_TDEP
#      define RETURN_CONST_KVISC
#   endif

#   ifdef  DENS_TDEP
#      define RETURN_CONST_DENS
#   endif

#   ifdef  THMCOND_TDEP
#     define RETURN_CONST_THMCOND
#   endif */
# endif

# ifndef  KVISC_TDEP
#   define   KIN_VISC      0.999028E-6  /* In "m^2/sec". */
# endif
# ifndef  DENS_TDEP
#   define   DENSITY       997.97       /* In "Kg/m^3". */
# endif
# ifndef  THMCOND_TDEP
#   define   COND_WATER    0.6          /* Water conductivity in 'W/m.K'. */
# endif
# define   Cp_WATER      4.172505E+3  /* Specific heat of water in 'J/Kg.K'. */
# define   W_MAX         0.12     /*0.121709*/     /* For parabolic profile at the inlet, Re = 600. */
# define   GRAVITY       9.81         /* Gravitational constant. */
# define   P_ATM         1.01E+5      /* In "Pa". */


/* Correct w_outlet for initial N_w_CORRECT number of steps so that "dmdt_in = dmdt_out". */
// #  define N_w_CORRECT 3

// # define   CORRECT_T_OUTLET

/*  If tn_BACKUP = 3, it stores physical quantities at time-instants of "t - dt" and "t - 2*dt in
    recovery file. Also, quantities at t, t-dt, and t-2*dt are used in application of Orlansky BC. */
# define  tn_BACKUP      3

/* Physical-domain velocities are stored specifically at a different memory location for zCells_BACKUP
   number of starting from the cell adjascent to the outlet boundary continuing along z-axis inside
   the physical domain. If zCells_BACKUP = 3, the quantities are saved for cell numbers NzG, NzG+1
   and NzG+2 at the outlet boundary. */
# define  zCells_BACKUP 3

struct arr_struct
   {
    double ****q;
    unsigned short nq;
    int n1, n2, n3;
   };

struct orlansky
   {
    double ***u[tn_BACKUP], ***v[tn_BACKUP], ***w[tn_BACKUP], **tnext_ug, **tnext_vg, **tnext_wg;
#   ifdef SOLVE_ENERGY
    double ***T[tn_BACKUP], **tnext_Tg;
#   endif
   };

struct grid_config
 {
# ifndef r_UNIF_GRID
  double r_mid1, r_mid2, dr_mid1, r_cd_center, r_cd_mid12, dr_base, dr_wall, r_cd_base, r_cd_gap;
  int nr_center, nr_mid12, nr_base;
# endif

# ifndef z_UNIF_GRID
  double z_mid1, z_mid2, z_cd_bot, z_cd_top, z_cd_mid12, dz_unif_bot;
  int nz_top, nz_mid1, nz_mid12;
# endif
 };


struct grid
 {
  int nr_nozzle, nr_solid, nr_gap, nr, nth, nz;
  double *r, *th, *z, ***u, ***v, ***w, ***um, ***vm, ***wm;

# ifdef r_UNIF_GRID
  double dr;
# endif

# ifdef th_UNIF_GRID
  double dth;
# endif

# ifdef z_UNIF_GRID
  double dz;
# endif


# ifdef SOLVE_ENERGY
  int nr_heater;
  double r_heater;
  double ***T;
  char vhsrc_flag;
# endif

  double ***p, /****div, */dt, t, density0, kvisc0, Re0, thmcond0;
  FILE *lptr, *trptr, *tptr, *dptr;

# ifndef DELTAt
  double cfl;
# endif

  struct orlansky outBC;

  short nTRD;
 };


/* Number of ghost-cells. */
# define     NrG        2
# define     NthG       2
# define     NzG        2

/* Degree of the polinomial used to approximate the ghost-cell w-velocity
   at the outlet. Used in "vel_bc()". */
// # define     VEL_INTRP_N    2

/* Degree of polynomial used to approximate the ghost-cell pressure
   at the outlet. Used in "press_bc()". */
# define     PRESS_INTRP_N  2

# ifdef SOLVE_ENERGY
 /* Degree of polynomial used to approximate the ghost-cell temperature at inlet in "temp_bc()". */
#   define   TEMP_INTRP_N   2

/* DEFINING r-COORDINATE RELATED QUANTITIES. */
#   define   d_HEATER   100.0E-3 /* Required condition: "0 <= d_HEATER <= r_CYL"; Modified. */
#   define   r_HEATER   (0.5*d_HEATER) /* Modified. */
# endif
# define     d_NOZZLE   9.85E-3
# define     d_BASE     200.0E-3
# define     d_CYL      273.5E-3
# define     r_CYL      (0.5*d_CYL)
# define     r_BASE     (0.5*d_BASE)
# define     r_NOZZLE   (0.5*d_NOZZLE)
# define     GAP        (0.5*(d_CYL - d_BASE))


# ifdef  r_UNIF_GRID
#   define   dR_CYL     0.985E-3 /* modified */
# else
#   define   dr_CENTER  0.3E-3
#   define   r_MID1     30.0E-3 /* modified */
#   define   dr_MID1    1.0E-3 /* modified */
#   define   dr_MID2    2.0E-3
#   define   r_MID2     80.0E-3 /* modified */
#   define   dr_BASE    1.5E-3 /* modified */
#   define   dr_WALL    2.0E-3 /* modified */
# endif


/* DEFINING theta-COORDINATE RELATED QUANTITIES. */
# ifdef th_UNIF_GRID

  // Cell-size in theta-direction in degrees.
#    define  dTHETA     5.0  /* modified */

  /* Keeping symmetry of the problem in mind, number of cells chosen in theta direction for computation is 'nTh_COMP'. */
#    define  nTH_COMP   5 /* It should be more than 2*NthG. */

# endif


/* DEFINING z-COORDINATE RELATED QUANTITIES. */
# define     h_CYL        58.6E-3 /* For "h/d_nozzle = 3"; Experiment: 58.6E-3. */
# define     h_WATER      h_CYL /* Height of the water column in the tank from the nozzle-exit. */
# define     z_TOP        h_CYL

# ifdef z_UNIF_GRID
#    define  dZ           9.75E-4 /* modified */
# else
#   define   dz_TOP       0.1E-3
#   define   dz_MID2      0.5E-3
#   define   z_MID2       50.0E-3 /* Modified */
#   define   z_MID1       20.0E-3 /* Modified */
#   define   dz_MID1      1.5E-3
#   define   dz_UNIF_BOT  0.5E-3 /* Modified */
#   define   nz_UNIF_BOT  1
# endif


/* Reynolds Number based on average velocity derived from Mass Flow Rate. */
// # define   Re            (0.5*W_MAX*d_NOZZLE/KIN_VISC)

#endif
